package com.MSofLectures.Entity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.MSofLectures.DatabaseBasic;
import com.MSofLectures.Operator.Lecture.OperatorOnLecture;
import com.MSofLectures.Operator.Lecture.OperatorOnLectureImp;
import com.MSofLectures.Operator.User.OperatorOnUser;
import com.MSofLectures.Operator.User.OperatorOnUserImp;

public abstract class Basic extends DatabaseBasic {
	private static final long serialVersionUID = 1L;
	/**
	 * @Title Basic.java
	 * @Package com.Entity
	 * @author ufo 这个包用来进行数据持久化以及处理一些和业务逻辑相关的读写
	 */

	protected Map<String, String[]> keyValueMap = new HashMap<String, String[]>();
	protected OperatorOnUserImp operatorOnUser = null;
	protected OperatorOnLectureImp operatorOnLecture = null;

	public String getValue(String key) {
		if (keyValueMap.size() <= 0)
			map();
		try {
			return keyValueMap.get(key)[0];
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	public abstract boolean map();

	public abstract void save();

	public String[] getValueArray(String key) {
		if (keyValueMap.size() <= 0)
			map();
		return keyValueMap.get(key);
	}

	public void altValue(String key, String value) {
		String[] tmp = getValueArray(key);
		if (null == tmp)
			tmp = new String[2];
		else
			tmp[0] = value;
		keyValueMap.put(key, tmp);
	}

	public void addKey(String key, String value) {
		String[] tmp = { value, null };
		keyValueMap.put(key, tmp);

	}

	public Map<String, String> iterator() {
		Map<String, String> result = new HashMap<String, String>();
		if(this.keyValueMap.isEmpty())
			map();
		Set<String> set = this.keyValueMap.keySet();
		for (Iterator<String> i = set.iterator(); i.hasNext();) {
			String tmp = (String) i.next();
			result.put(tmp, this.keyValueMap.get(tmp)[0]);
		}
		return result;
	}

	// 数据持久化的关键对象，所有的数据持久化都会通过这个对象来完成
	public void finalize() throws Throwable {
		System.out.println("Entity.Basic Collect");
		if (null != this.operatorOnLecture)
			this.operatorOnLecture.finalize();
		if (null != this.operatorOnUser)
			this.operatorOnUser.finalize();
		super.finalize();
	}

	public OperatorOnUserImp getOperatorOnUser() {
		if (null == this.operatorOnUser)
			this.operatorOnUser = new OperatorOnUser();
		return this.operatorOnUser;
	}

	public void setOperatorOnUser(OperatorOnUserImp operatorOnUser) {
		this.operatorOnUser = operatorOnUser;
	}

	public OperatorOnLectureImp getOperatorOnLecture() {
		if (null == this.operatorOnLecture)
			this.operatorOnLecture = new OperatorOnLecture();
		return this.operatorOnLecture;
	}

	public void setOperatorOnLecture(OperatorOnLectureImp operatorOnLecture) {
		this.operatorOnLecture = operatorOnLecture;
	}

	public Map<String, String[]> getKeyValueMap() {
		return keyValueMap;
	}

	public void setKeyValueMap(Map<String, String[]> keyValueMap) {
		this.keyValueMap = keyValueMap;
	}
}
